<template>
  <div class="app-main-container">
    <QueryForm
      v-model="filters"
      :query-form-config="queryFormConfig"
      @handleQuery="getList(1)"
      @clear="clearCondition"
    />
    <div
      v-if="meta.indexOf('SaveOrUpdateByVenue') > -1"
      class="doumee-table-buttons"
    >
      <el-button type="primary" @click="handleEdit">{{ $t('message.add') }}</el-button>
    </div>
    <el-table
      ref="table"
      v-loading="listLoading"
      :data="list"
      element-loading-text="Loading"
      border
      fit
      :header-row-class-name="'table-header'"
      class="doumee-element-table doumee-element-tableb"
    >
      <el-table-column align="center" :label="$t('message.zxkmc')" min-width="120" show-overflow-tooltip>
        <template v-if="meta.indexOf('DetailByVenue') > -1" slot-scope="scope">
          <span
            class="primaryColor pointer"
            @click="handleDetail(scope.row.id)"
          >{{ scope.row.name }}</span>
        </template>
      </el-table-column>
      <el-table-column
        v-for="item in propList"
        :key="item.prop"
        align="center"
        :prop="item.prop"
        :label="item.label"
        :min-width="item.minWidth"
      />
      <el-table-column
        align="center"
        :label="$t('message.xssd')"
        min-width="120"
        show-overflow-tooltip
      >
        <template slot-scope="scope">
          <div v-if="scope.row.sellTimeType == '0'">{{ $t('message.cqsm') }}</div>
          <div v-else>
            <div>{{ $t('message.qi') }}：{{ scope.row.sellTimeStart }}</div>
            <div>{{ $t('message.zhi3') }}：{{ scope.row.sellTimeEnd }}</div>
          </div>
        </template>
      </el-table-column>
      <el-table-column align="center" :label="$t('message.status')" min-width="80" fixed="right">
        <template slot-scope="scope">
          <el-switch
            v-model="scope.row.status"
            v-preventReClick
            active-color="#435EBE"
            inactive-color="#999999"
            :active-value="'1'"
            :inactive-value="'0'"
            :disabled="meta.indexOf('HandleByVenue') == -1 || scope.row.isVenue != 1"
            @change="changeStatus(scope.row)"
          />
        </template>
      </el-table-column>
      <el-table-column
        :label="$t('message.caozuo')"
        min-width="120"
        align="center"
        fixed="right"
      >
        <template slot-scope="scope">
          <div v-if="scope.row.isVenue == 1" class="flex-cc">
            <template v-if="scope.row.status == '0'">
              <el-button
                v-if="meta.indexOf('SaveOrUpdateByVenue') > -1"
                v-preventReClick
                type="text"
                @click="handleEdit(scope.row)"
              >{{ $t('message.edit') }}</el-button>
              <el-button
                v-if="meta.indexOf('HandleByVenue') > -1"
                v-preventReClick
                type="text"
                style="color: red"
                @click="deleteCard(scope.row.id)"
              >{{ $t('message.del') }}</el-button>
            </template>
            <el-button
              v-if="
                meta.indexOf('SaveOrUpdateByVenue') > -1 &&
                  scope.row.status == '1'
              "
              v-preventReClick
              type="text"
              @click="handleEdit(scope.row, 'copy')"
            >{{ $t('message.copy') }}</el-button>
          </div>
          <span v-else>-</span>
        </template>
      </el-table-column>
    </el-table>
    <Pagination
      :total="total"
      :page="pagination.page"
      @current-change="getList"
      @SizeChange="handleSizeChange"
    />
    <Detail v-if="isShowDetail" ref="detailRef" @close="isShowDetail = false" />
    <Edit
      v-if="isShowEdit"
      ref="editRef"
      @close="isShowEdit = false"
      @success="getList"
    />
  </div>
</template>

<script>
import {
  SpeailCardList,
  SpeailCardUpdate
} from '@/api'
import { parseTime } from '@/utils/index'
import Pagination from '@/components/paginations'
import { queryFormConfig, propList } from './config'
import Edit from './edit.vue'
import Detail from './detail.vue'
export default {
  name: 'SpecialCardList',
  components: {
    Pagination,
    Edit,
    Detail
  },
  data() {
    return {
      isShowEdit: false,
      isShowDetail: false,

      list: [],
      queryFormConfig,
      propList,
      listLoading: true,
      meta: [],
      total: 0,
      pagination: {
        page: 1,
        rows: 10
      },
      filters: {}
    }
  },
  created() {
    this.meta = this.$route.meta.buttons || []
    this.getList(1)
  },
  mounted() {
    this.$nextTick(() => {
      this.$refs.table.doLayout()
    })
  },
  activated() {
    this.$nextTick(() => {
      this.$refs.table.doLayout()
    })
  },
  methods: {
    handleSizeChange(value) {
      this.pagination.rows = value
      this.getList(1)
    },
    handleEdit(row, str) {
      this.isShowEdit = true
      this.$nextTick(() => {
        this.$refs.editRef.isShowDrawer = true
        if (row && row.id) {
          this.$refs.editRef.getDetail(row.id, str)
        }
      })
    },
    handleDetail(id) {
      this.isShowDetail = true
      this.$nextTick(() => {
        this.$refs.detailRef.isShowDrawer = true
        this.$refs.detailRef.getDetail(id)
      })
    },
    clearCondition() {
      // 清除条件
      this.filters = {}
      this.getList(1)
    },
    editCard(id) {
      // 编辑卡片
      // console.log(id)
      this.$router.push({ name: 'VipCardEdit', query: { id: id }})
    },
    changeStatus(v) {
      // 编辑卡片状态
      const obj = { ...v }
      const arr = this.list
      const that = this
      SpeailCardUpdate({ param: { id: obj.id, status: obj.status, type: '1' }}).then(res => {
        if (res.errorCode === '000000') {
          const str = obj.status === '1' ? this.$t('message.qy') : this.$t('message.jy')
          that.$message.success(str + this.$t('message.success'))
          that.getList()
        } else {
          that.list = arr
          that.getList()
        }
      }).catch(() => {
        that.getList()
      })
    },
    deleteCard(id) {
      // 删除卡片
      this.$confirm(this.$t('message.sczxktc'), this.$t('message.ts'), {
        confirmButtonText: this.$t('message.qd'),
        cancelButtonText: this.$t('message.qx'),
        type: 'warning'
      })
        .then(() => {
          SpeailCardUpdate({
            param: {
              id: id,
              type: '2'
            }
          }).then(res => {
            if (res.errorCode === '000000') {
              this.getList()
            }
          })
        })
        .catch(() => {
          this.getList(1)
          // this.$message.info('已取消删除')
        })
    },
    getList(page) {
      // 获取列表
      this.listLoading = true
      const { filters, pagination } = this
      if (page) {
        pagination.page = page
        this.pagination.page = page
      }
      if (pagination.page === 1) {
        pagination.firstQueryTime = parseTime(new Date())
      }
      SpeailCardList({
        pagination,
        param: { ...filters }
      })
        .then((res) => {
          this.listLoading = false
          if (res.errorCode === '000000') {
            this.list = res.recordList.map(item => {
              item.typeName = item.type === '1' ? this.$t('message.zyk') : this.$t('message.lpk')
              item.validTime = item.validTime ? item.validTime + this.$t('message.tian') : this.$t('message.cqyx')
              item.circulationAll = item.circulationAll ? item.circulationAll : this.$t('message.bxl')

              return item
            })
            this.total = res.totalCount
          }
        })
        .catch(() => {
          this.listLoading = false
        })
    }
  }
}
</script>
